package com.example.service.implService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.config.ResponseVO;
import com.example.dao.ClassesDao;
import com.example.mapper.ClassesMapper;
import com.example.mapper.TeacherMapper;
import com.example.pojo.Classes;
import com.example.pojo.TeacherInfo;
import com.example.service.ClassesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class ClassesServiceImpl implements ClassesService {
    @Resource
    private ClassesDao classesDao;
    @Resource
    private ClassesMapper classesMapper;
    @Resource
    private TeacherMapper teacherMapper;
    @Override
    public List<Classes> getAllClasses() {
        return classesDao.getAllClasses();
    }


    @Override
    public ResponseEntity<ResponseVO<List<Classes>>> classesInfoListDistribute(int gradeLevel) {
        QueryWrapper<Classes> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("grade_level",gradeLevel);
        //先查出来全部
        List<Classes> list =classesMapper.selectList(queryWrapper);
        // 创建一个新列表来存储筛选后的班级
        List<Classes> filteredClasses = new ArrayList<>();
        //遍历全部数据，得到每个班的人数限制
        for (Classes classes : list) {
            //在对查出的数据进行筛选/得到班级人数不大于最大限制的人
            System.out.println("改班级最对容纳："+classes.getMaxStudentCapacity()+"当前人数："+classes.getCurrentStudentCount());
            //筛选
            if (classes.getCurrentStudentCount() < classes.getMaxStudentCapacity()){
                filteredClasses.add(classes);
            }
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK,"获取成功",filteredClasses));

    }

    @Override
    public IPage<Classes> getClassesPage(int currentPage, int limit) {
        return classesDao.getClassesPage(currentPage,limit);
    }

    @Override
    public Classes getClassInfoById(Long classId) {
        return classesDao.getClassInfoById(classId);
    }

    @Override
    public String addOrUpdateClasses(Classes classes) {
        return classesDao.addOrUpdateClasses(classes);
    }

    @Override
    public String deleteClasses(Long classesId) {
        return classesDao.deleteClasses(classesId);
    }

    @Override
    public ResponseEntity<ResponseVO<List<Long>>> getTeacherByClassesId(Long classId) {
        Classes classes = classesMapper.selectById(classId);
        List<Long> teacherIds = new ArrayList<>();
        if(classes.getHeadTeacherId()!=null){
            teacherIds.add(classes.getHeadTeacherId());
        }
        if (classes.getAssistantTeacherId() != null) {
            teacherIds.add(classes.getAssistantTeacherId());
        }
        if(teacherIds.isEmpty()){
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK,"获取成功",teacherIds));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST,"数据获取失败"));
    }
}
